package ru.nsu.ccfit.shumkina.algorithm.adaptation.profitcounter;

import ru.nsu.ccfit.shumkina.algorithm.adaptation.Path;
import ru.nsu.ccfit.shumkina.algorithm.adaptation.UsedPathsInfo;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * User: vera
 * Date: 5/15/13
 * Time: 7:20 PM
 * To change this template use File | Settings | File Templates.
 */
public class AdaptationSummaryPrinter {
    public static void printSummaryToFile(String fileName, Profit profit, TreeMap<String, UsedPathsInfo> usedPaths) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
            bw.write("Initial number of moves: ");
            bw.write(profit.allMoves.toString());
            bw.newLine();
            bw.write("Number of moves needed after adaptation: ");
            Integer i1 = profit.allMoves - profit.savedMoves;
            bw.write(i1.toString());
            bw.newLine();
            bw.write("Number of moves decreased by ");
            Integer i = (profit.savedMoves*100)/profit.allMoves;
            bw.write(i.toString());
            bw.write("%");
            bw.newLine();
            bw.newLine();
            bw.write("Number of added arcs: ");
            bw.write(String.valueOf(profit.adaptedSubpaths.size()));
            bw.newLine();
            bw.write("New arcs:");
            bw.newLine();


            for(Map.Entry<String, ArrayList<String>> entry: profit.adaptedSubpaths.entrySet())
            {
                bw.write(entry.getKey());
                bw.write("\t");
                bw.write("adaptation for ");
                bw.write(entry.getValue().toString());
                bw.newLine();
            }

            bw.newLine();
            bw.write("Statistic of used paths");
            for (Map.Entry<String, UsedPathsInfo> entry: usedPaths.entrySet())
            {
                for (Map.Entry<String, Path> pathEntry: entry.getValue().paths.entrySet())
                {
                    bw.newLine();
                    bw.write(pathEntry.getValue().path.toString());
                    bw.write(" path");
                    bw.newLine();
                    bw.write("adapted to: \t");
                    bw.write(pathEntry.getValue().adaptedPath.toString());
                    bw.newLine();
                    bw.write("use count: \t");
                    bw.write(String.valueOf(pathEntry.getValue().useCount));
                    bw.newLine();
                    bw.write("length decrease: \t");
                    bw.write(String.valueOf(pathEntry.getValue().path.size()
                            - pathEntry.getValue().adaptedPath.size()));
                    bw.newLine();
                    bw.write("moves decrease: \t");
                    bw.write(String.valueOf((pathEntry.getValue().path.size()
                            - pathEntry.getValue().adaptedPath.size())*pathEntry.getValue().useCount));
                }
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        //bw.write("digraph graphname {");
       // bw.newLine();

    }
}
